<?php
namespace app\client\controller;
use app\client\wxpay\Config;
use app\client\wxpay\WxRefund;

class Refund {
    public function chargeRefund() {
        $params = request()->post();
        $orderId = isset($params['id']) ? intval($params['id']) : 0;
        if($orderId <= 0){
            return errorResult('abc');
        }
        $orderModel = model('ChargeOrder');
        $order = $orderModel->findOne(['order_id' => $orderId], ['refund_status', 'status', 'amount', 'order_no', 'trade_no', 'pay_to', 'agent_id']);
        if(empty($order) || $order['refund_status'] != 0) {
            return errorResult('ddd');
        }
        
        $config = new Config();
        
        if($order['pay_to'] == 2){
            $agent = model('Agent')->findOne(['agent_id' => $order['agent_id']], ['mch_id', 'mch_key']);
            $config->setMerchantId($agent['mch_id']);
            $config->setKey($agent['mch_key']);
        }
        $order['amount'] = 0.01;
        $orderNo = $order['order_no'];
        $tradeNo = $order['trade_no'];
        $totalAmount = $order['amount'];
        $refundAmount = isset($params['amount']) ? floatval($params['amount']) : $order['amount'];
        if($refundAmount > $totalAmount) {
            return errorResult('ddd');
        }
        
        $refundSuccess = false;
        $result = WxRefund::refund($tradeNo, $orderNo, $totalAmount, $refundAmount, $config);
        if($result['success'] == false){ //第一次退款失败
            var_dump($result);
            if($result['err_code'] === 'SYSTEMERROR' || $result['err_code'] === 'BIZERR_NEED_RETRY'){ //再退一次
                $result = WxRefund::refund($tradeNo, $orderNo, $totalAmount, $refundAmount, $config);
                if($result['success'] == true){
                    $refundSuccess = true;
                }
            }
        }else{
            $refundSuccess = true;
        }
        
        if($refundSuccess == false) { //退款失败
            model('RefundErrorLog')->add(['order_no' => $orderNo, 'trade_no' => $tradeNo, 'msg' => $result['msg'], 'err_code' => $result['err_code'], 'type' => 1, 'add_time' => date('Y-m-d H:i:s', time())]);
            return errorResult('ccc');
        }else{
            $orderModel->modify(['order_id' => $orderId], ['refund_amount' => $refundAmount, 'refund_status' => 1]);
            return successResult();
        }
    }
    
    //增加时间退款
    public function extendChargeRefund() {
        $orderId = request()->post('id', 0, 'intval');
        if($orderId <= 0){
            return errorResult();
        }
        
        $orderModel = model('ExtendChargeOrder');
        $order = $orderModel->findOne(['order_id' => $orderId], ['refund_status', 'status', 'amount', 'order_no', 'trade_no', 'charge_order_id']);
        if($order['refund_status'] != 0) {
            return errorResult();
        }
        
        $config = new Config();
        
        $chargeOrder = model('ChargeOrder')->findOne(['order_id' => $order['charge_order_id']], ['agent_id', 'pay_to']);

        if($chargeOrder['pay_to'] == 2){
            $agent = model('Agent')->findOne(['agent_id' => $chargeOrder['agent_id']], ['mch_id', 'mch_key']);
            $config->setMerchantId($agent['mch_id']);
            $config->setKey($agent['mch_key']);
        }
        $order['amount'] = 0.01;
        $orderNo = $order['order_no'];
        $tradeNo = $order['trade_no'];
        $amount = $order['amount'];
        
        $refundSuccess = false;
        $result = WxRefund::refund($tradeNo, $orderNo, $amount, $amount, $config);
        if($result['success'] == false){
            if($result['err_code'] === 'SYSTEMERROR' || $result['err_code'] === 'BIZERR_NEED_RETRY') { //再退一次
                $result = WxRefund::refund($tradeNo, $orderNo, $amount, $amount, $config);
                if($result['success'] == false){
                    $refundSuccess = true;
                }
            }
        }else{
            $refundSuccess = true;
        }
        
        if($refundSuccess == false) { //退款失败
            model('RefundErrorLog')->add(['order_no' => $orderNo, 'trade_no' => $tradeNo, 'msg' => $result['msg'], 'err_code' => $result['err_code'], 'type' => 2, 'add_time' => date('Y-m-d H:i:s', time())]);
            return errorResult('ccc');
        }else{
            $updateData = ['refund_status' => 1];
            if($order['status'] != 2){
                $updateData['status'] = 2;
                
            }
            $orderModel->modify(['order_id' => $orderId], $updateData);
            
            return successResult();
        }
        
    }
}




